package com.lottery.material.service.admin.banner.impl;

import com.lottery.material.config.upload.UploadFileConfig;
import com.lottery.material.dao.banner.BannerConfMapper;
import com.lottery.material.dao.banner.BannerJumpTypeDictMapper;
import com.lottery.material.dao.conf.MenuLevelConfigMapper;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.pojo.banner.BannerConf;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojocustom.banner.BannerConfCustom;
import com.lottery.material.service.admin.banner.BannerConfService;
import com.lottery.material.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Description：banner配置服务接口实现类
 * Created by 朱正磊 on 2019-04-13 14:38
 */
@Service("adminBannerConfService")
public class BannerConfServiceImpl implements BannerConfService {

    private final BannerConfMapper bannerConfMapper;

    private final MenuLevelConfigMapper menuLevelConfigMapper;

    private final BannerJumpTypeDictMapper bannerJumpTypeDictMapper;

    private final UploadFileConfig uploadFileConfig;

    @Autowired
    public BannerConfServiceImpl(BannerConfMapper bannerConfMapper,
                                 MenuLevelConfigMapper menuLevelConfigMapper,
                                 BannerJumpTypeDictMapper bannerJumpTypeDictMapper,
                                 UploadFileConfig uploadFileConfig) {
        this.bannerConfMapper = bannerConfMapper;
        this.menuLevelConfigMapper = menuLevelConfigMapper;
        this.bannerJumpTypeDictMapper = bannerJumpTypeDictMapper;
        this.uploadFileConfig = uploadFileConfig;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param request 请求对象
     * @param bannerConf 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(HttpServletRequest request, BannerConf bannerConf) {
        if (AssertUtil.isNotEmpty(request)) { //其实可以不用加判断
            //设置banner的有效期
            this.setTime(request, bannerConf);
            //设置banner图片
            this.setPictureByOSS(request, bannerConf);
        }
        bannerConf.setCreateTime(new Date());
        bannerConf.setState((Integer) CommonEnum.STATE_OFF.getValue()); //默认下架
        return bannerConfMapper.insertSelective(bannerConf);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return bannerConfMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public BannerConf findById(Integer id) {
        return bannerConfMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param request 请求对象
     * @param bannerConf 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(HttpServletRequest request, BannerConf bannerConf) {
        if (AssertUtil.isNotEmpty(request)) { //判断必须加
            //设置banner的有效期
            this.setTime(request, bannerConf);
            //设置banner图片
            this.setPictureByOSS(request, bannerConf);
        }
        bannerConf.setUpdateTime(new Date());
        return bannerConfMapper.updateByPrimaryKeySelective(bannerConf);
    }

    /**
     * description：设置banner的有效期
     * user 朱正磊
     * time 2019-05-22 09:24
     * @param request 请求对象
     * @param bannerConf 业务对象
     */
    private void setTime(HttpServletRequest request, BannerConf bannerConf) {
        String startTimeStr = request.getParameter("startTimeStr");
        bannerConf.setStartTime(CalendarUtil.stringToDate(startTimeStr));
        String endTimeStr = request.getParameter("endTimeStr");
        bannerConf.setEndTime(CalendarUtil.stringToDate(endTimeStr));
    }

    /**
     * description： 设置banner图片（使用OSS）
     * user 朱正磊
     * time 2019-04-23 09:43
     * @param request 请求对象
     * @param bannerConf 业务对象
     */
    private void setPictureByOSS(HttpServletRequest request, BannerConf bannerConf) {
        //采用OSS作为图片服务器
        String pictureAddressOne = OSSClientUtil.uploadFiles(uploadFileConfig,
                FileUploadUtil.getMultipartFiles(request, "pictureAddressOne"), uploadFileConfig.getBannerConfModuleName());
        if (AssertUtil.isNotEmpty(pictureAddressOne)) {
            //设置banner图片
            bannerConf.setImageUrl(pictureAddressOne);
        }
    }

    /**
     * description： 设置图片（使用nginx）
     * user 朱正磊
     * time 2019-04-23 09:43
     * @param request 请求对象
     * @param bannerConf 业务对象
     */
    @SuppressWarnings("unused")
    private void setPictureByNginx(HttpServletRequest request, BannerConf bannerConf) {
        //采用nginx作为图片服务器
        String pictureAddressOne = FileUploadUtil.getPictureUrl(request, uploadFileConfig.getProjectName(),
                uploadFileConfig.getBannerConfModuleName(), "pictureAddressOne", FtpUtil.getFtpConfig(uploadFileConfig));
        if (AssertUtil.isNotEmpty(pictureAddressOne)) {
            //设置banner图片
            bannerConf.setImageUrl(pictureAddressOne);
        }
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<BannerConfCustom> findCustomListPage(Page page) {
        List<BannerConfCustom> list = bannerConfMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public BannerConfCustom findCustomById(Integer id) {
        BannerConfCustom custom = bannerConfMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(BannerConfCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //开始时间
        custom.setStartTimeFormat(CalendarUtil.dateToString(custom.getStartTime()));
        //结束时间
        custom.setEndTimeFormat(CalendarUtil.dateToString(custom.getEndTime()));
        //修改时间
        custom.setUpdateTimeFormat(CalendarUtil.dateToString(custom.getUpdateTime()));
        //状态（1：上架；2：下架）
        custom.setStateDesc(EnumUtil.getDescriptionByCodeValue(CommonEnum.STATE, custom.getState(), CommonEnum.class));
        //所属模块名称（一级类别）
        String menuName = menuLevelConfigMapper.findMenuNameByMenuCode(custom.getMenuCode());
        custom.setMenuName(menuName);
        //跳转类型名称
        String jumpTypeName = bannerJumpTypeDictMapper.findNameByCode(custom.getJumpTypeCode());
        custom.setJumpTypeName(jumpTypeName);
    }
    
}
